<?php
// Base Modules configuration class
class BaseModules
{

    // For singleton
    private static $instance = false;

    // Vars
    protected $modules = array();
    protected $actions = array();
    protected $defaultModule;
    protected $modulesPath;
    protected $templatesPath;

    public $currentModule;
    public $currentAction;

    // Constructor for modules configuration and singleton to use anywhere
    public function __construct()
    {
        self::$instance = $this;
        $this->loadPaths();
    }

    public static function getInstance()
    {
        if (!self::$instance)
        {
            die("NO PREVIOUS INSTANCE!");
        }
        return self::$instance;
    }

    // Load modules path
    protected function loadPaths()
    {
        $this->modulesPath   = BASE.'modules/';
        $this->templatesPath = BASE.'templates/';
    }

    // Set module
    protected function setModule($modName, $modUrl, $isDefault = false)
    {
        $this->modules[$modName] = array(
            'url_prefix'     => $modUrl,
            'module_path'    => $this->modulesPath.$modName,
            'templates_path' => $this->templatesPath.$modName,
            'controller'     => ucfirst($modName).'Controller',
            'model'          => ucfirst($modName).'Model',
            'view'           => ucfirst($modName).'View',
            'module_name'    => $modName,
        );
        $this->actions[$modName] = array();
        if ($isDefault)
        {
            $this->defaultModule = $modName;
        }
    }

    // Set action
    protected function setAction($module, $actionName, $actionUrl, $isDefault = false)
    {
        if (isset($this->actions[$module]))
        {
            if ($isDefault)
            {
                $this->modules[$module]['default_action'] = $actionName;
            }
            $this->actions[$module][$actionName] = $actionUrl;
        }
        else
        {
            die("SET ACTION FAILED ($actionName). Module $module is not defined!");
        }
    }

    // Get module
    public function getModule($modName)
    {
        if (!empty($this->modules[$modName]))
        {
            return $this->modules[$modName];
        }
        else
        {
            return false;
        }
    }

    // Get action
    public function getAction($modName, $action)
    {
        if (!empty($this->actions[$modName][$action]))
        {
            return $this->actions[$modName][$action];
        }
        else
        {
            return false;
        }
    }

    // Get all actions
    public function getActions()
    {
        return $this->actions;
    }

    // Get default module
    public function getDefaultModule()
    {
        if (isset($this->defaultModule))
        {
            return $this->modules[$this->defaultModule];
        }
        else
        {
            if (count($this->modules) > 0)
            {
                reset($this->modules);
                $firstModule = key($this->modules);
                $this->defaultModule = $firstModule;
                return $this->modules[$this->defaultModule];
            }
            else
            {
                die("NO MODULES!");
            }
        }
    }

    // Get default action
    public function getDefaultAction($modName)
    {
        if (!empty($this->actions[$modName]))
        {
            if (!empty($this->modules[$modName]['default_action']))
            {
                return $this->modules[$modName]['default_action'];
            }
            else
            {
                if (count($this->actions[$modName]) > 0)
                {
                    reset($this->actions[$modName]);
                    $firstAction = key($this->actions[$modName]);
                    $this->modules[$modName]['default_action'] = $firstAction;
                    return $this->actions[$modName][$this->modules[$modName]['default_action']];
                }
                else
                {
                    die("NO ACTIONS IN MODULE!");
                }
            }
        }
        else
        {
            die("NO ACTIONS IN THIS MODULE!");
        }
    }

    // Set & get current module and action functions below
    public function setCurrentModuleAndAction($module, $action)
    {
        $this->currentModule = $module;
        $this->currentAction = $action;
    }

    public function getCurrentModule()
    {
        return $this->currentModule;
    }

    public function getCurrentAction()
    {
        return $this->currentAction;
    }
}
?>